document.write("<script src=\"https://cdn.staticfile.org/axios/0.18.0/axios.min.js\"></script>");
var apiUrl = 'http://localhost:8081/';

function StandardPost(url, args) {
    var form = $("<form method='post'></form>"),
        input;
    form.attr({
        "action": url
    });
    $.each(args, function (key, value) {
        input = $("<input type='hidden'>");
        input.attr({
            "name": key
        });
        input.val(value);
        form.append(input);
    });
    $(document.body).append(form);
    form.submit();
}

function StandardGet(url) {
    var datas = {}
    $.ajax({
        url: apiUrl + url,
        dataType: 'json',
        type: "get",
        traditional: true,
        async: false,
        success: function (data) {
            datas = data
            // console.log(datas)
        },
        error: function () {
            console.log("接口错误")
        },

    })
    return datas;

}

function StandardPost_get(url, args) {
    var datas = {}
    $.ajax({
        url: apiUrl + url,
        dataType: 'json',
        type: "post",
        traditional: true,
        data: args,
        async: false,
        success: function (data) {
            datas = data
            // console.log(datas)
        },
        error: function () {
            console.log("接口错误")
        },

    })
    return datas;
}

let api = {
    post: (url, args) => {
        return new Promise((resolve, reject) => {
            axios({
                url: apiUrl + url,
                method: 'post',
                data: args,
                transformRequest: [function (data) {
                    // Do whatever you want to transform the data
                    let ret = ''
                    for (let it in data) {
                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                    }
                    return ret
                }],
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(res => {
                resolve(res.data);
                // console.log('请求结果：', res);
            }).catch(res => {
                resolve(res.data);
            });

        })
    },
    get: (url) => {
        return new Promise((resolve, reject) => {
            axios({
                url: apiUrl + url,
                method: 'get'
            }).then(res => {
                resolve(res.data);
                // console.log('请求结果：', res);
            }).catch(res => {
                resolve(res.data);
            });

        })
    },
    postbyhost: (host, url, args) => {
        return new Promise((resolve, reject) => {
            axios({
                url: host + url,
                method: 'post',
                data: args
            }).then(res => {
                resolve(res.data);
                // console.log('请求结果：', res);
            }).catch(res => {
                resolve(res.data);
            });

        })
    },
    getbyhost: (host, url) => {
        return new Promise((resolve, reject) => {
            axios({
                url: host + url,
                method: 'post'
            }).then(res => {
                resolve(res.data);
                // console.log('请求结果：', res);
            }).catch(res => {
                resolve(res.data);
            });

        })
    }

}


function StandardGet_get_host(host, url, args) {
    var datas = {}
    $.ajax({
        url: host + url,
        dataType: 'json',
        type: "post",
        traditional: true,
        data: args,
        async: false,
        headers: {},
        success: function (data) {
            datas = data
            // console.log(datas)
        },
        error: function () {
            console.log("接口错误")
        },

    })
    return datas;

}


